package com.pss.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pss.order.config.rabbitmq.Sender;
import com.pss.order.dao.BOrderMapper;
import com.pss.order.entity.BOrder;
import com.pss.order.entity.UndoLog;
import com.pss.order.service.OrderService;
import com.pss.order.service.UndoLogService;
import com.pss.order.service.openFeign.ProductFeignService;
import io.seata.spring.annotation.GlobalTransactional;
//import org.apache.shardingsphere.transaction.annotation.ShardingTransactionType;
//import org.apache.shardingsphere.transaction.annotation.ShardingTransactionType;
import org.apache.shardingsphere.transaction.annotation.ShardingTransactionType;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl extends ServiceImpl<BOrderMapper, BOrder> implements OrderService {

    @Autowired
//    @LoadBalanced
    private ProductFeignService productFeignService;

    @Autowired
    private BOrderMapper bOrderMapper;

    @Autowired
    private Sender sender;

    @Autowired
    private UndoLogService undoLogService;

    @Override
    @GlobalTransactional
    public BOrder submitOrder(Integer pId) {
        BOrder bOrder = new BOrder();
        bOrder.setOrderNumber(String.valueOf(System.currentTimeMillis()));
        bOrder.setProductId(pId);
        bOrderMapper.insert(bOrder);
        System.out.println("下单"+pId);
//        ExpHandler.throwBaseException("订单服务自定义异常");
        String s = productFeignService.productInfo(pId);
        return bOrder;
    }

    @Override
    public void sendMq() {
        List<Map> list = new ArrayList<>();
        Map map = new HashMap();
        map.put("msg", "你好");
        map.put("code", 0);
        list.add(map);
        sender.sendDhSms(list);
    }

    @Override
    @Transactional
    @ShardingTransactionType(TransactionType.BASE)  // 支持TransactionType.LOCAL, TransactionType.XA, TransactionType.BASE
    public List<BOrder> submitOrder3(List<BOrder> order) {
        saveBatch(order);
        String s = productFeignService.productInfo(order.get(0).getProductId());
        return order;
    }

    @Override
    public PageInfo<BOrder> selectAll(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<BOrder> users = list();
        PageInfo<BOrder> pageInfo = new PageInfo<>(users);
        return pageInfo;

    }
}
